<?php

	class AttrClassControl extends AuthControl{
		public function index(){
		
			// $id = Q('gt_id',0,'intval');
			// if($id){
			// 	$model = K('GoodsAttr');
			// 	$data = $model->showAttr($id);
			// 	$this->assign('gt_id',$id);
			// 	if($data){
			// 		$this->assign('data',$data);

			// 		$this->display();
			// 	}else{
			// 		$this->error('还没有为该商品类型添加属性');
			// 	}
			// }else{
			// 	$this->error('参数错误');
			// }
			$id = Q('gt_id',0,'intval');
			$gtname = Q('gtname');
			$model = K('AttrClass');
			$data = $model->showAttr($id);
			$this->assign('gt_id',$id);
			$this->assign('gtname',$gtname);
			if($data){
				$this->assign('data',$data);
				$this->display();
			}else{
				$this->error('还没有为该商品类型添加属性',U('add',array('gt_id'=>$id)));
			}
		

			
		}
		public function add(){
			$id = Q('gt_id');
			$this->assign('gt_id',$id);
			if(IS_POST){
				$model = K('AttrClass');
				if($model->addAttr()){
					$this->success('属性添加成功',U('index',array('gt_id'=>$id)));

				}else{

					$this->error($model->error);
				}
			}else{
				$gt_id = Q('gt_id');
				$this->assign('gt_id',$gt_id);

				$this->display();
			}
		}

		public function showFieldTpl(){
			$show_type = Q('show_type');
			$tpl = APP_PATH.'Field/'.$show_type.'/add.php';
			$content = $this->fetch($tpl);
			echo $content;exit;
		}

		public function save(){
			if(IS_POST){
				$ac_id = Q('ac_id');
				$model = K('AttrClass');
				if($model->saveAttr($ac_id)){
					if($model->delAttrValue($ac_id)){
						if($model->addAttrValue($ac_id,$_POST['attr_value'])){
							$this->success('商品属性值更新成功',U('index',array('gt_id'=>Q('gt_id'))));
						}else{
							$this->error($model->error);
						}

					}else{
						$this->error($model->error);
					}
				}else{
					$this->error($model->error);
				}

			}else{
				$ac_id = Q('ac_id',0,'intval');
				if($ac_id){
					$model = K('AttrClass');
					$attrClassData = $model->where(array('ac_id'=>$ac_id))->all();//获得当前ac_id 的记录
					$show_type = $attrClassData[0]['show_type']; //得到显示类型
					$FieldTpl = APP_PATH.'Field/'.$show_type.'/edit.php'; //由显示类型找到显示的模板

					//获得属性值
					$attrValueData = M('attr_value')->where(array('ac_id'=>$ac_id))->all();
					$this->assign('attrValueData',$attrValueData);
					$this->assign('attrClassData',$attrClassData);  //将当前ac_id的记录分配到save模板
					$this->assign('FieldTpl',$FieldTpl);       //将显示模板路径分配到save模板
					$this->display();
				}else{
					$this->error('没有属性ID');
				}
			}
		}
		public function del(){
			$ac_id = Q('ac_id');
			$gt_id = Q('gt_id');
			if(!empty($ac_id)){
				//删除 attr_value表的数据
				$model = K('AttrClass');
				if($model->delAttr($ac_id)){
					$this->success('删除成功',U('index',array('gt_id'=>$gt_id)));
				}else{
					$this->error($model->error);
				}
			}else{
				$this->error('ac_id参数不正确');
			}
		}
	}
?>
